//
//  XMGPlaceholderTextView.m
//  百思不得姐
//
//  Created by Mine on 16/8/22.
//  Copyright © 2016年 Min. All rights reserved.

/**   注意：
 * setNeedsDisplay方法 : 会在恰当的时刻自动调用drawRect:方法
 * setNeedsLayout方法 : 会在恰当的时刻调用layoutSubviews方法
 */


#import "XMGPlaceholderTextView.h"

@interface XMGPlaceholderTextView()
/** 占位文字label */
@property (nonatomic, weak) UILabel *placeholderLabel;
@end

@implementation XMGPlaceholderTextView

- (UILabel *)placeholderLabel
{
    if (!_placeholderLabel) {
        // 添加一个用来显示占位文字的label
        UILabel *placeholderLabel = [[UILabel alloc] init];
        placeholderLabel.numberOfLines = 0;
        placeholderLabel.x = 4;
        placeholderLabel.y = 7;
        [self addSubview:placeholderLabel];
        _placeholderLabel = placeholderLabel;
    }
    return _placeholderLabel;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // 垂直方向上永远有弹簧效果
        self.alwaysBounceVertical = YES;
        
        // 默认字体
        self.font = [UIFont systemFontOfSize:15];
        
        // 默认的占位文字颜色
        self.placeholderColor = [UIColor grayColor];
        
        // 监听文字改变
        [XMGNoteCenter addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

- (void)dealloc
{
    [XMGNoteCenter removeObserver:self];
}

/**
 * 监听文字改变
 */
- (void)textDidChange
{
//    [self setNeedsDisplay]; 1.0
    // 只要有文字, 就隐藏占位文字label  2.0
     self.placeholderLabel.hidden = self.hasText;
}

//--------1.0--------//
///**
// * 绘制占位文字(每次drawRect:之前, 会自动清除掉之前绘制的内容)
// */
//- (void)drawRect:(CGRect)rect {
//    // 如果有文字, 直接返回, 不绘制占位文字
//    //    if (self.text.length || self.attributedText.length) return;
//    if (self.hasText) return;
//    
//    // 处理rect
//    rect.origin.x = 4;
//    rect.origin.y = 7;
//    rect.size.width -= 2 * rect.origin.x;
//    
//    // 文字属性
//    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
//    attrs[NSFontAttributeName] = self.font;
//    attrs[NSForegroundColorAttributeName] = self.placeholderColor;
//    [self.placeholder drawInRect:rect withAttributes:attrs];
//}

/**
 * 更新占位文字的尺寸 2.0
 */
//- (void)updatePlaceholderLabelSize
//{
//    CGSize maxSize = CGSizeMake(XMGScreenW - 2 * self.placeholderLabel.x, MAXFLOAT);
//    self.placeholderLabel.size = [self.placeholder boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:nil].size;
//    self.placeholderLabel.backgroundColor = [UIColor redColor];
//}

/**
 * 更新占位文字的尺寸 2.0   8月23日
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.placeholderLabel.width = self.width - 2 * self.placeholderLabel.x;
    [self.placeholderLabel sizeToFit];
}

#pragma mark - 重写setter
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    
//    [self setNeedsDisplay];
    self.placeholderLabel.textColor = placeholderColor;
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = [placeholder copy];
    
//    [self setNeedsDisplay];
    self.placeholderLabel.text = placeholder;
    [self setNeedsLayout];

}

- (void)setFont:(UIFont *)font
{
    [super setFont:font];
    
//    [self setNeedsDisplay];
    
    self.placeholderLabel.font = font;
    [self setNeedsLayout];
}

- (void)setText:(NSString *)text
{
    [super setText:text];
    
    [self textDidChange];
}

- (void)setAttributedText:(NSAttributedString *)attributedText
{
    [super setAttributedText:attributedText];
    
    [self textDidChange];
}

@end
